//
//  Global.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/10.
//  Copyright © 2020 lt. All rights reserved.
//

import Foundation
import UIKit
import Kingfisher
import SnapKit
import MJRefresh
import MBProgressHUD

//应用默认颜色
extension UIColor {
    
    //class 关键字允许子类重写属性
    class var background: UIColor {
        return UIColor(r: 242, g: 242, b: 242)
    }
    
    class var theme: UIColor {
        return UIColor(r: 29, g: 221, b: 43)
    }
    
}

//MARK: Kingfisher
extension Kingfisher where Base: ImageView {
    @discardableResult
    public func setImage(urlString: String?, placeholder: Placeholder? = UIImage(named: "normal_placeholder_h")) -> RetrieveImageTask {
        return setImage(with: URL(string: urlString ?? ""),
                        placeholder: placeholder,
                        options:[
                            .transition(.fade(0.5))
        ])
    }
}

var topVC: UIViewController? {
    var resultVC: UIViewController?
    resultVC = _topVC(UIApplication.shared.keyWindow?.rootViewController)
    while resultVC?.presentedViewController != nil {
        resultVC = _topVC(resultVC?.presentedViewController)
    }
    return resultVC
}

private  func _topVC(_ vc: UIViewController?) -> UIViewController? {
    if vc is UINavigationController {
        return _topVC((vc as? UINavigationController)?.topViewController)
    } else if vc is UITabBarController {
        return _topVC((vc as? UITabBarController)?.selectedViewController)
    } else {
        return vc
    }
}

extension String {
    static let sexTypeKey = "sexTypeKey"
    static let searchHistoryKey = "searchHistoryKey"
}

//通知
extension NSNotification.Name{
    static let USexTypeDidChange = NSNotification.Name("USexTypeDidChange")
}

//屏幕
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height


//刘海屏额外的高度
let heightOfAddtionalHeade:CGFloat = {
    if UIDevice.current.isiPhoneXorLater(){
        return 24.0
    }else {
        return 0.0
        
    }
}()


//底部非安全区域高度
let heightOfAddtionalFooter:CGFloat = {
    if UIDevice.current.isiPhoneXorLater(){
        return 34.0
    }else {
        return 0.0
    }
}()


//toast
func showToast(_ text:String,view:UIView){
    //只显示文字
    let hud = MBProgressHUD.showAdded(to: view, animated: true)
    hud.mode = MBProgressHUDMode.text
    hud.label.text = text
    hud.removeFromSuperViewOnHide = true
    hud.hide(animated: true, afterDelay: 1.5)
}



//MARK: SnapKit
extension ConstraintView {
    
    var usnp: ConstraintBasicAttributesDSL {
        if #available(iOS 11.0, *) {
            return self.safeAreaLayoutGuide.snp
        } else {
            return self.snp
        }
    }
}
